#! /usr/bin/python
DB_NAME = 'taras_sina'
DB_USER = 'taras'
DB_PASS = 'admin123'
DB_HOST = 'localhost'

import os, sys, cPickle
sys.path.append('/home/yayuan/codebase/')
sys.path.append('/home/yayuan/codebase/taras/sdk/')
import taras.sql_agent
import taras.sdk.weibopy.oauth as oauth

print "Content-type: text/html\n\n"

query = os.environ['QUERY_STRING']
query = dict(i.split('=') for i in query.split('&'))

identity = query['identity']
user_id = query['user_id']
app_id = query['app_id']
oauth_token = query['oauth_token']
oauth_secret = query['oauth_token_secret']
token = oauth.OAuthToken(oauth_token, oauth_secret)

agent = taras.sql_agent.init(DB_NAME, DB_USER, DB_PASS, DB_HOST)

agent.start()

agent.app_auth_token.add({'user_id': int(user_id),
                          'app_id': int(app_id),
                          'value': cPickle.dumps(token)}, force=True)

agent.stop()

print "%s updated to DB" % identity
